package yxy.game.pm2.command.battle;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BattleMessage;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.battle.BattleModule;
import yxy.game.pm2.module.battle.BattleRecord;

/** 战斗 战斗播放 */
@MessageType(proto = Command.BattlePlay, classtype = BattleMessage.BattlePlayRequest.class)
public class CMD_BattlePlay extends Command<BattleMessage.BattlePlayRequest> {

	@Override
	public void execute(MessageSource messageSource, BattleMessage.BattlePlayRequest request) {
		String playerCode = messageSource.getTag();
		String battleCode = request.getBattleCode();

		BattleMessage.BattlePlayResponse.Builder response = BattleMessage.BattlePlayResponse.newBuilder();

		BattleRecord record = BattleModule.getAndSave(playerCode, battleCode);
		if (record == null) {
			record = BattleRecord.getv(battleCode);
			if (record == null) {
				send(messageSource, response, Result.BattleRecordExpired);//录像已经过期了
				return;
			}
		}
		response.setPlayback(new BattleHandle().playback(record));
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, BattleMessage.BattlePlayResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}